#include "login.h"

namespace gdata
{

gClient_login::gClient_login(const string& serviceName, 
                             const string appName)
{
    _serviceName = serviceName;
    _appName = appName;

    _requestHeaders.push_back("User-Agent: " + _appName);
    _requestHeaders.push_back("GData-Version: 2.0");
}


string gClient_login::client_login(const string accountName,
                        const string password,
                        const string accountType = "HOSTED_OR_GOOGLE")
{
    string aData = "Email=" + accountName + 
                      "&Passwd=" + password +
                      "&accountType=" + accountType +
                      "&source=" + _appName +
                      "&service=" + _serviceName;
    user_data usrData;
    usrData.content = const_cast<char *>(aData.c_str());
    usrData.size = aData.size();

    https aHttps;
    string resLogin = aHttps.https_request("POST", clientLoginUrl, usrData);

    string prefix = "Auth=";
    string aToken = resLogin.substr(resLogin.find(prefix) + prefix.size());

    _authToken = clientLoginAuthPrefix + aToken.substr(0, aToken.size() - 1);

    return _authToken;
}


}
